﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using mmoss.orm;
using System.Xml;

using Artem.Google.Net;

namespace mmoss.classes
{
    public static class Mapping
    {
        public static void loadLatLong(Patient l_patient)
        {
            if (l_patient.patientAddressLat == null)
            {
                bool bChanged = false;
                if (l_patient.patientAddress.Trim() != "" && l_patient.patientCity != "" && l_patient.patientState != "")
                {
                    //string l_address = l_patient.patientAddress.Trim().Replace(' ', '+') + ",+" + l_patient.patientCity.Trim().Replace(' ', '+') + ",+" + l_patient.patientState.Trim();
                    string l_address = "";
                    if (l_patient.patientAddress.Trim().Replace(".", "").Substring(0, 2).ToUpper() == "PO")
                    {
                        l_address = l_patient.patientCity.Trim() + ", " + l_patient.patientState.Trim();
                    }
                    else
                    {
                        l_address = l_patient.patientAddress.Trim() + " " + l_patient.patientCity.Trim() + ", " + l_patient.patientState.Trim();
                    }
                    try
                    {
                        GeoRequest request = new GeoRequest(l_address);
                        GeoResponse response = request.GetResponse();
                        if (response.Results.Count > 0)
                        {
                            GeoLocation location = response.Results[0].Geometry.Location;
                            double latitude = location.Latitude;
                            double longitude = location.Longitude;
                            l_patient.patientAddressLat = decimal.Parse(Math.Round(latitude, 6).ToString());
                            l_patient.patientAddressLong = decimal.Parse(Math.Round(longitude, 6).ToString());
                        }
                    }
                    catch (Exception ex)
                    {
                        //do nothing now
                    }
                }
            }
        }


        public static double distance(double lat1, double lon1, double lat2, double lon2, char unit)
        {
            double theta = lon1 - lon2;
            double dist = Math.Sin(deg2rad(lat1)) * Math.Sin(deg2rad(lat2)) + Math.Cos(deg2rad(lat1)) * Math.Cos(deg2rad(lat2)) * Math.Cos(deg2rad(theta));
            dist = Math.Acos(dist);
            dist = rad2deg(dist);
            dist = dist * 60 * 1.1515;
            if (unit == 'K')
            {
                dist = dist * 1.609344;
            }
            else if (unit == 'N')
            {
                dist = dist * 0.8684;
            }
            return (dist);
        }

        //:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
        //::  This function converts decimal degrees to radians             :::
        //:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
        private static double deg2rad(double deg)
        {
            return (deg * Math.PI / 180.0);
        }

        //:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
        //::  This function converts radians to decimal degrees             :::
        //:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
        private static double rad2deg(double rad)
        {
            return (rad / Math.PI * 180.0);
        }    
    
    }

}
